package com.org.platform.support.config;

import com.org.platform.support.error.MyErrorPageRegistrar;
import java.util.concurrent.TimeUnit;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

/**
 * @author duheng
 * @Date 2021/11/13 15:20
 */
@Configuration
public class BasicConfig {

	@Bean
	public RestTemplate restTemplate() {
		Integer timeOut = 30000;
		// 长链接保持时间长度20秒
		PoolingHttpClientConnectionManager poolingHttpClientConnectionManager =
				new PoolingHttpClientConnectionManager(20, TimeUnit.SECONDS);
		int cpuCore = Runtime.getRuntime().availableProcessors();
		// 设置最大链接数
		poolingHttpClientConnectionManager.setMaxTotal(900);
		// 单路由的并发数
		poolingHttpClientConnectionManager.setDefaultMaxPerRoute(900);
		HttpClientBuilder httpClientBuilder = HttpClients.custom();
		httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
		// 重试次数3次，并开启
		httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true));
		HttpClient httpClient = httpClientBuilder.build();
		// 保持长链接配置，keep-alive
		httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
		HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
		// 链接超时配置 5秒
		httpComponentsClientHttpRequestFactory.setConnectTimeout(timeOut);
		// 连接读取超时配置
		httpComponentsClientHttpRequestFactory.setReadTimeout(timeOut);
		// 连接池不够用时候等待时间长度设置，分词那边 500毫秒 ，我们这边设置成1秒
		httpComponentsClientHttpRequestFactory.setConnectionRequestTimeout(timeOut);
		// 缓冲请求数据，POST大量数据，可以设定为true 我们这边机器比较内存较大
		httpComponentsClientHttpRequestFactory.setBufferRequestBody(true);
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.setRequestFactory(httpComponentsClientHttpRequestFactory);
		restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
		return restTemplate;
	}

	@Bean
	public ErrorPageRegistrar errorPageRegistrar() {
		return new MyErrorPageRegistrar();
	}
}
